package com.test.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.test.domain.TAccountuser;
import com.test.service.InformService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.Company;
import com.test.domain.User;
import com.test.service.CompanyService;
import com.test.service.RoleService;
import com.test.service.UserService;
import com.test.vo.Result;

@Controller
@RequestMapping("inform")
public class InformController {

    @Autowired
    private InformService informService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;

    @RequirePermission("inform:listUI")
    @RequestMapping("listUI")
    public String listUI(HttpServletRequest request) {
        return "inform/listUI";
    }

    @RequirePermission("inform:listUI")
    @RequestMapping("list")
    @ResponseBody
    public Result list(int offset, int limit,String search) {
        PageInfo<TAccountuser> pageInfo = this.informService.getInformListByPage(offset / limit + 1, limit, search);
        return Result.succeed(pageInfo);
    }
    
    @RequirePermission("companyinform:theListUI")
    @RequestMapping("theListUI")
    public String theListUI(HttpServletRequest request) {
        return "companyinform/theListUI";
    }

    @RequirePermission("companyinform:theListUI")
    @RequestMapping("theList")
    @ResponseBody
    public Result theList(int offset, int limit,String search,HttpSession session) {
    	Object userObj = session.getAttribute("loginUser");
    	String companyid="";
		if (userObj != null) {
			companyid=((User)userObj).getCompanyid().toString();
			System.out.println("公司id："+companyid);
		}
        PageInfo<TAccountuser> pageInfo = this.userService.getCompanyInformListByPage(offset / limit + 1, limit, search,companyid);
        return Result.succeed(pageInfo);
    }
    @RequestMapping("inform")
	public String toRegister(Model model) {
			List<Company> companyList=companyService.getList();
			model.addAttribute("companyList",companyList);
			model.addAttribute("count",companyList.size());
			return "inform";
		//return "redirect:/inform.jsp";
		//return "user/saveUI";
	}
    @RequestMapping("saveUI")
    public String saveUI(Integer id,HttpServletRequest request) {
    	if (id != null) {
    		TAccountuser auser = this.userService.getInformById(id);
    		if (auser != null) {
    		    System.out.println(auser.toString());
    			request.setAttribute("auser", auser);
    		}
    	}
    	return "inform/saveUI";
    }
    //修改
    @RequirePermission("inform:add;inform:update")
    @RequestMapping("save")
    public String add(TAccountuser accountuser) {
        if (accountuser.getAuserId() != null) {
            Date date=new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(df.format(date));
            accountuser.setAnswertime(new Date());
            accountuser.setIsanswer(1);
            this.informService.setAdvice(accountuser);

        }
        return "redirect:/inform/listUI";
    }

}
